<?php
$err = NULL;
if(dyRequirePriv(SYSTEM))
{

function ClassMgmtRegister($clsid, $name, $attrib)
{
	// Validation
	if((strlen($clsid) != 32) || (!ctype_alnum($clsid)))
		return EINVALIDARGS;
	echo "a";
	if(strlen($name) > 50)
		return EINVALIDARGS;
		
		
	$obj = new cDycmsAttributes();
	if(!$obj)
		return ENOMEM;
	
	// Parse attributes
	
	$attrib = split("\n", $attrib);
	
	foreach($attrib as $cur)
	{
		$cur = split(",", $cur);
		if(count($cur) != 4)
			return EINVALIDARGS;
		
		if(!ctype_alnum($cur[0]))
			return EINVALIDARGS;
		if(($cur[1] != "int") && ($cur[1] != "string"))
			return EINVALIDARGS;
		if(!ctype_digit($cur[2]))
			return EINVALIDARGS;
		
		$obj->Add($cur[0], $cur[1], $cur[2], ($cur[3] == "index" ? true : false));
	};
	
	$obj->Dump();
	
	if(dbWriteClass($clsid, $name, $obj))
		return ENONE;
	else
		return EDB;
}

$action = $_GET["action"];
if($action == NULL)
{
?>
	<h1>Class Management</h1>
	<ul>
		<li><a href="admin.php?mod=classmgmt&action=install">Install Class</a></li>
		<li><a href="admin.php?mod=classmgmt&action=view">View Installed Classes</a></li>
		<li><a href="admin.php?mod=classmgmt&action=reg">Register Class</a></li>
	</ul>
<?php
}
else if($action == "view")
{
	uiErrorCode(ENOTIMPLEMENTED);
}
else if($action == "install")
{
	uiErrorCode(ENOTIMPLEMENTED);
}
else if($action == "reg")
{
	$clsid = $_POST["clsid"];
	$name = $_POST["name"];
	$attrib = $_POST["attrib"];
	
	if($clsid && $name && $attrib)
	{
		$e = ClassMgmtRegister($clsid, $name, $attrib);
		if($e == ENONE)
		{
			uiMessage("AddSuccess");
		}
		else
		{
			uiErrorCode($e);
			dyLogDump();
		};
	}
	else
	{
?>
	<h1>Register Class</h1>
	<form action="admin.php?mod=classmgmt&action=reg" method="post">
		<table border="0" cellpadding="0" cellspacing="0" class="form">
          <tr>
            <td><div align="left">Class ID:</div></td>
            <td><input name="clsid" onChange="formValidate('usr');" type="text" class="frmvalid" /></td>
          </tr>
          <tr>
            <td><div align="left">Class Name:</div></td>
            <td><input name="name" onChange="formValidate('pwd');" type="text" class="frmvalid" id="pwd" /></td>
          </tr>
          <tr>
            <td colspan="2"><div align="left">Attributes (one per line, format: name,int/string,len,indexed[yes/no])</div></td>
          </tr>
		  <tr>
			<td colspan="2">
			  <textarea name="attrib" cols="60" rows="10"></textarea>
			</td>
          </tr>
          <tr>
            <td colspan="2"><div align="center"><input type="submit" value="Create"></div></td>
          </tr>
        </table> 
	</form>
<?php
	}
};
};
?>